import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const storeObj = {
    modules: {
        user: {
            namespaced: true,
            // state 用于存储状态
            state: {
                count: 0,
                getcount: 0,
                todos: [
                    { id: 1, title: 'vue', done: true },
                    { id: 2, title: 'vuex', done: true },
                    { id: 3, title: 'vue-router', done: false },
                    { id: 4, title: 'element-ui', done: true },
                ]
            },
            mutations: {
                increment(state) {
                    state.count++
                },
                incrementCount(state, n) {
                    state.getcount += n
                }
            },
            // getters 相当于计算属性 可以用于改变
            getters: {
                doneTodos: state => {
                    return state.todos.filter(todo => todo.done)
                },
                doneTodosCount: (state, getters) => {
                    return getters.doneTodos.length
                },
                getTodoById: (state) => (id) => {
                    return state.todos.find(todo => todo.id === id)
                }
            },
            actions: {
                increment(context) {
                    context.commit('increment')
                },
                incrementObj({ commit }) {
                    commit('increment')
                },
                checkout({ commit, state }, products) {
                    console.log(commit)
                    console.log(state)
                    commit('incrementCount', products.amount)

                }
            }
        }
    }
}

const store = new Vuex.Store(storeObj)

export default store